package com.tian.service.impl;

import com.tian.entity.ExchangeCode;
import com.tian.mapper.ExchangeCodeMapper;
import com.tian.message.UserUseCouponExchangeCodeMessage;
import com.tian.service.UserUseCouponExchangeCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * {@code @description:} TODO
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-11-09 14:54
 * {@code @version:} 1.0
 */
@Slf4j
@Service
public class UserUseCouponExchangeCodeServiceImpl implements UserUseCouponExchangeCodeService {
    @Resource
    private ExchangeCodeMapper exchangeCodeMapper;
    @Override
    public void process(UserUseCouponExchangeCodeMessage userUseCouponExchangeCodeMessage) {
        ExchangeCode exchangeCode = exchangeCodeMapper.selectByPrimaryKey(userUseCouponExchangeCodeMessage.getCouponId());
        if (exchangeCode == null) {
            // 兑换码不存在
            log.error("此优惠券id:{}不存在兑换码", userUseCouponExchangeCodeMessage.getCouponId());
            return;
        }
        if (exchangeCode.getStatus() == 1) {
            // 兑换码已使用
            log.error("此兑换码:{}已使用", exchangeCode.getCode());
            return;
        }
        // 兑换码未使用，更新兑换码状态
        exchangeCode.setStatus(userUseCouponExchangeCodeMessage.getStatus());
        exchangeCode.setUpdateTime(new Date());
        exchangeCode.setUserId(userUseCouponExchangeCodeMessage.getUserId());
        exchangeCode.setOrderId(userUseCouponExchangeCodeMessage.getOrderId());
        exchangeCodeMapper.updateByPrimaryKey(exchangeCode);
        // 处理成功，发送兑换码已使用消息
        log.info("兑换码:{}已使用", exchangeCode.getCode());
    }
}
